package hadoop.mapjoin;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class DistributedCacheDriver {
    public static void main(String[] args) throws IOException, URISyntaxException, ClassNotFoundException, InterruptedException {
        //获取Job信息
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //设置加载Jar包路径
        job.setJarByClass(DistributedCacheDriver.class);
        //
        job.setMapperClass(DistributedCacheMapper.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //设置输入输出路径
        FileSystem fs =FileSystem.get(conf);
        Path outputpath = new Path("D:\\apache-maven-3.6.3\\workOutput\\hadoop\\mapJoin\\output");
        if(fs.exists(outputpath)){
            fs.delete(outputpath,true);
        }
        FileInputFormat.setInputPaths(job,new Path("D:\\apache-maven-3.6.3\\mavenRepository\\mapReduce\\dataSet\\inputtable\\order.txt"));
        FileOutputFormat.setOutputPath(job,outputpath);

        //加载缓存数据
        job.addCacheFile(new URI("file:///D:/apache-maven-3.6.3/mavenRepository/mapReduce/dataSet/inputtable/pd.txt"));
        //mapJoin不需要Reduce阶段，所以设置reduceTask数为0
        job.setNumReduceTasks(0);

        //提交
        Boolean isDone = job.waitForCompletion(true);
        System.exit(isDone?0:1);
    }
}
